package com.kingyun.gpsinspection.purificationservice.services.iam.taskjobservice;
import com.kingyun.gpsinspection.purificationservice.common.enums.DeviceSumTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.scyx.DeviceSumTempInfoMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.scyx.DeviceSumTempInfo;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.scyx.DeviceSumInfoService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 年类型定时任务数据同步
 * Created by jiangjx on 2017/2/27.
 */
@Service
public class DeviceSumMonthJobService implements Job {
    public Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DeviceSumTempInfoMapper deviceSumTempInfoMapper;
    @Autowired
    private DeviceSumInfoService deviceSumInfoService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<DeviceSumTempInfo> list = this.getDeviceSumTempInfoList();
        if(list !=null && list.size() >0){
            int len = list.size();
            for (int i = 0; i < len; i++) {
                DeviceSumTempInfo deviceSumTempInfo = list.get(i);
                deviceSumInfoService.insertSelective(deviceSumTempInfo.getDeviceSumTempId());
            }
        }else{
            logger.info("未获取到年台账数据");
        }
        if (list == null || list.size() < 0) return;
        int len = list.size();
        for (int i = 0; i < len; i++) {
            DeviceSumTempInfo deviceSumTempInfo = list.get(i);
            deviceSumInfoService.insertSelective(deviceSumTempInfo.getDeviceSumTempId());
        }
    }

    /**
     * 获取所有年台账模板数据
     *
     * @return
     */
    protected List<DeviceSumTempInfo> getDeviceSumTempInfoList() {
        List<DeviceSumTempInfo> list = null;
        DeviceSumTempInfo deviceSumTempInfo = new DeviceSumTempInfo();
        Integer sumType = DeviceSumTypeEnum.DEVICE_SUM_TYPE_ENUM_3.getCode();
        deviceSumTempInfo.setDeviceSumType(Short.parseShort(sumType.toString()));
        try {
            list = deviceSumTempInfoMapper.queryListNotPading(deviceSumTempInfo);
        } catch (Exception e) {
            logger.error("同步台账数据时,获取年台账数据异常:"+e.getMessage() );
            e.printStackTrace();
        }
        return list;
    }
}
